package com.frog.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class RandomStringUtil {
	/** 考虑到多处公用，可能会引起线程性能问题，因此采用ThreadLocalRandom  **/
	public static String randomString(int length) {
		StringBuilder sb = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			int nextInt = ThreadLocalRandom.current().nextInt(2);
			if (nextInt % 2 == 0) { // 数字
				sb.append(ThreadLocalRandom.current().nextInt(10));
			} else {// 字母
				nextInt = ThreadLocalRandom.current().nextInt(2);
				if (nextInt % 2 == 0) { // 小写
					sb.append((char) ('a' + ThreadLocalRandom.current().nextInt(26)));
				} else {
					sb.append((char) ('A' + ThreadLocalRandom.current().nextInt(26)));
				}
			}
		}
		return sb.toString();
	}
	
	/** 随机生成指定长度的数字 字符串，并且首字符不为 0  **/
	public static String randomIntString(int length) {
		StringBuilder sb = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			int num = ThreadLocalRandom.current().nextInt(10);
			if (i == 0 && num == 0) {
				length ++ ;
				continue;
			}
			sb.append(num);
		}
		return sb.toString();
	}
	
	public static List<Long> numbersString2LongList(String numbersString){
		if(numbersString == null) {
			return new ArrayList<>();
		}
		numbersString = numbersString.trim();
		int length = numbersString.length();
		StringBuilder number = new StringBuilder();
		boolean isInNumber = false;
		List<Long> numbersList = new ArrayList<>();
		for(int i = 0;i<length;i++) {
			/**
			 * 数字和字母的范围为：
			 * 1.  0-9 : 48-57
			 * 2.  A-Z : 65-90
			 * 3.  a-z : 97-122
			 */
			int charAt = (int) numbersString.charAt(i);
			if(charAt >= 48 && charAt <= 57) {
				if(!isInNumber) {
					isInNumber = true;
				}
				number.append((char)charAt);
			}else {
				if(isInNumber) {
					isInNumber = false;
					numbersList.add(Long.parseLong(number.toString()));
					number.setLength(0);
				}
			}
		}
		if(number.length() > 0) {
			numbersList.add(Long.parseLong(number.toString()));
		}
		return numbersList;
	}

}
